import api from './api.js'
import { get, post,patchFuc,deleteFuc } from './http.js'

let request = {};
import Cookies from 'js-cookie';

import { useRouter } from 'vue-router';
api.map((item) => {
  let name = item.name;
  request[name] = (params) => {
    let url = item.base.url;
    let method = item.base.method;
    if (!params) {
      params = {}
    }
      item.base.headers.Authorization = 'Bearer ' + Cookies.get('user-tk')
      if(!item.base.headers.token || item.base.headers.token.length == 0) {
        // const router = useRouter();
        // router.replace({
        //   name: 'Login'
        // })
        // return new Promise((resolve, reject) => {
        //   reject({
        //     code: 401,
        //   });
        // });
      }
    if(item.config.replaceid) {
      url = url.replace('{id}', params.id1?params.id1:params.id)
      delete params.id
      delete params.id1
      if(params.id2){
        params.id=params.id2
        delete params.id2
      }
    }
    switch (method) {
      case 'post': {
        return post(url, params, item.config, item.base.headers);
      }
      case 'patch': {
        return patchFuc(url, params, item.config, item.base.headers);
      }
      case 'delete': {
        return deleteFuc(url, params, item.config, item.base.headers);
      }
      // 默认是get
      default: {
        return get(url, params, item.config, item.base.headers);
      }
    }
  }
});
export default request
